class Tools::WeatherForecastsController < ApplicationController  
  before_filter :authorize, :except => :index_in_show
  
  def index
    @setting = UserToolSetting.find(params[:setting_id])
    @area = @setting.area
    @weather_info = @area.weather_info
    @today_weather_info = @area.today_weather_info
    @alerts = Info.find_all_by_area_from_rss(@setting.area)
  end
  
  def edit
    edit_in_show
  end
  
  def update
    @setting = UserToolSetting::WeatherForecast.find(params[:id])
    @tool = @setting.tool
    if @setting.update_attributes(params["setting_#{@setting.id}"])
      redirect_to :action => :index, :format => :js, :setting_id => @setting.id
    else
      @province = Province.first
      render :edit
    end
  end
  
  def copy
    @setting  = UserToolSetting::WeatherForecast.find(params[:id])
    @new = UserToolSetting::WeatherForecast.new(
      @setting.attributes.keys.each_with_object({}) do |key,hash|
        hash[key] = @setting.attributes[key] unless [:id,
          :created_at,
          :updated_at,
          :emails_count,
          :sms_count         
        ].include?(key)
      end
    )
    @new.save
    @settings = UserToolSetting.all_by_current_user_by_column @new.column
  end
  
  def destroy
    @setting = UserToolSetting::WeatherForecast.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
    
  def index_in_show
    @tool = Tool.find_by_code('weather_forecast')
    @settings = UserToolSetting::WeatherForecast.find_all_by_user_id_and_tool_id(current_user_id, @tool.id)
  end
  
  def new_in_show
    @tool = Tool.find_by_code('weather_forecast')
    @province = Province.first
    @setting = UserToolSetting::WeatherForecast.new(:column => @tool.default_column)
  end
  
  def create_in_show    
    @tool = Tool.find_by_code('weather_forecast')
    @setting = UserToolSetting::WeatherForecast.new(params[:user_tool_setting_weather_forecast])
    @setting.attributes = {
      :user_id => current_user_id,
      :tool_id => @tool.id
    }
    if @setting.save
      redirect_to :action => :index_in_show, :format => :js
    else
      @province = Province.first
      render :new_in_show
    end
  end
  
  def edit_in_show    
    @setting = UserToolSetting::WeatherForecast.find(params[:id])
    @tool = Tool.find_by_code('weather_forecast')
    @province = @setting.city.province
  end
  
  def update_in_show
    @setting = UserToolSetting::WeatherForecast.find(params[:id])
    @tool = @setting.tool
    if @setting.update_attributes(params[:user_tool_setting_weather_forecast])
      redirect_to :action => :index_in_show, :format => :js
    else
      @province = Province.first
      render :edit_in_show
    end
  end
  
  def destroy_in_show
    @setting = UserToolSetting::WeatherForecast.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
  
  def send_email
    send_email_and_render do 
      UserToolSetting::WeatherForecast.find(params[:id]).send_email!
    end
  end
  
  def send_sm
    send_sm_and_render do
      UserToolSetting::WeatherForecast.find(params[:id]).syn_send_sm!(:by => current_user.fetion_version)
    end
  end
end
